// pages/picture/picture.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    i: 0,
    actual_price: '',
    all_price: '',
    service_name: [''],
    item_name: '',
    picPath: '',
    isis: false
  },
  dao: function (e) {
    this.setData({
      isis: !this.data.isis
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  ChooseImag: function (e) {
    var that = this
    wx.chooseImage({
      count: 1,
      sourceType: 'album',
      sizeType: ['compressed'],
      success(res) {
        that.setData({
          tempFilePaths: res.tempFilePaths[0]

        })
        setTimeout(function (e) {
          wx.showLoading({
            title: '正在上传图片...',
          })
        }, 500)
        let MyFile = new wx.BaaS.File()
        let fileParams = {
          filePath: that.data.tempFilePaths
        }
        let metaData = {
          categoryName: 'SDK',
          categoryName: 'image'
        }

        MyFile.upload(fileParams, metaData).then(res => {
          // 上传成功
          console.log(res)
          that.setData({
            avatarUrl: res.data.path,
            picPath: res.data.path,
            pic_id: res.data.file.id
          })
          wx.hideLoading()
          console.log(that.data.avatarUrl)
          // let tableName = 'service_items'
          // let recordID = wx.getStorageSync('id') // 数据行 id
          // let Product = new wx.BaaS.TableObject(tableName)
          // let product = Product.getWithoutData(recordID)

          // product.set('fukuanma', that.data.avatarUrl);
          // wx.setStorageSync('isInformation2', true)
          // product.update().then(res => {
          //     console.log(res)
          // }, err => {
          //     // err
          // })
        }, err => {
          // HError 对象
          // this.hideLoading()
          wx.showModal({
            title: '上传失败',
            content: '请等待三秒后重新点击<提交>',
            showCancel: false,
            success: res => {
              if (res.confirm) {
                that.setData({
                  submitDisabled: true
                })
                setTimeout(function () {
                  that.setData({
                    submitDisabled: false
                  })
                }, 5000)
              }
            }
          })
        })

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  plusName: function (e) {
    console.log(e)
    this.data.service_name.push('')
    this.setData({
      service_name: this.data.service_name
    })
  },
  submit: function () {

    if (this.data.dao == true) {
      // 设置方式一
      let tableName = 'service_items'
      let Product = new wx.BaaS.TableObject(tableName)
      let product = Product.create()
      let apple = {
        item_pic: this.data.picPath,
        item_name: this.data.item_name,
        actual_price: this.data.actual_price,
        all_price: this.data.all_price,
        service_name: this.data.service_name,
        isxianxia: 1
      }
      product.set(apple).save().then(res => {
        // success
        console.log(res)
      }, err => {
        //err 为 HError 对象
      })
    } else {
      let tableName = 'service_items'
      let Product = new wx.BaaS.TableObject(tableName)
      let product = Product.create()
      let apple = {
        item_pic: this.data.picPath,
        item_name: this.data.item_name,
        actual_price: this.data.actual_price,
        all_price: this.data.all_price,
        service_name: this.data.service_name,
        // isxianxia: 1
      }
      product.set(apple).save().then(res => {
        // success
        console.log(res)
      }, err => {
        //err 为 HError 对象
      })
    }


   
  },
  deletePic: function () {
    let MyFile = new wx.BaaS.File()

    MyFile.delete(this.data.pic_id).then()
    this.setData({
      picPath: ''
    })

  },
  deleteName: function (e) {
    console.log(e)
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认删除',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var tempIndex = e.target.dataset.id
          that.data.service_name.splice(tempIndex, 1)
          console.log(that.data.service_name.join(''))

          var conactArr = that.getNewArr()
          console.log("名字", conactArr)
          that.setData({
            service_name: conactArr
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  getNewArr: function (e) {
    var conactArr = []
    for (var i = 0; i < this.data.service_name.length; i++) {
      conactArr = conactArr.concat(this.data.service_name[i])
    }
    return conactArr
  },
  setYJ: function (e) {
    this.setData({
      all_price: e.detail.value
    })
    console.log(this.data.all_price)
  },
  setXJ: function (e) {
    this.setData({
      actual_price: e.detail.value
    })
    console.log(this.data.actual_price)
  },
  setMC: function (e) {
    this.setData({
      item_name: e.detail.value
    })
    console.log(this.data.item_name)
  },
  setJSMC: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id
    this.data.service_name[id] = e.detail.value
    this.setData({
      service_name: this.data.service_name
    })
    console.log(this.data.service_name)
  },

})